package SERVER;



import com.sun.javafx.scene.control.GlobalMenuAdapter;

import java.io.*;
import java.net.*;
/**
 * Klasa GameTable - klasa starująca przebiegiem gry
 * @author Miroslaw
 *
 */
public class GameTable 
{
	Deck deck;
	int chips_number;
	int big_blind;
	int small_blind;

	GameTable(int players_number, int chips_number, int big_blind, int small_blind) throws BadugiException
	{
		if(2>players_number || players_number>6)
		{
			throw new BadugiException("Players number have to be from 2 to 6");
		} 
		else if (chips_number<100)
		{
			throw new BadugiException("Chips number must be higher than 100");

		}
		else if (big_blind>chips_number)
		{
			throw new BadugiException("Big blind have to be smaller than Chips number");
		}
		else if(small_blind>=big_blind)
		{
			throw new BadugiException("small blind have to be smaller than big blind");
		}
		else
		{
			GlobalVar.players_number = players_number;
			this.chips_number=chips_number;
			this.small_blind=small_blind;
			this.big_blind=big_blind;
		}

	}
	public void CheckTheGame()
	{
		if(GlobalVar.server_status==2)
		{
			int i = ServerStart.player.size() - 1;
			sendOne("S "+String.valueOf(GlobalVar.server_status)+" "+String.valueOf(GlobalVar.players_number),i );
		}
		else if(GlobalVar.players_connected<GlobalVar.players_number)
		{
			GlobalVar.server_status=0;
			sendAll("ServerS " + String.valueOf(GlobalVar.server_status) + " " + String.valueOf(GlobalVar.players_number));
		}
		else if(GlobalVar.players_connected==GlobalVar.players_number)
		{
			GlobalVar.server_status=1;
			sendAll("ServerS "+String.valueOf(GlobalVar.server_status)+" "+String.valueOf(GlobalVar.players_number));
			GameStart();
		}
		else if(GlobalVar.players_connected>GlobalVar.players_number)
		{
			GlobalVar.server_status=2;
			sendAll("ServerS "+String.valueOf(GlobalVar.server_status)+" "+String.valueOf(GlobalVar.players_number));
		}
	}

	public void GameStart()
	{
		deck = new Deck();
		setPlayerIndex();//ustawienie graczom numerów
		for(int i=0; i<ServerStart.player.size();i++)
		{
			sendOne("I "+String.valueOf(i)+" "+String.valueOf(GlobalVar.players_number), i);
		}

		for(int i=0; i<ServerStart.player.size(); i++)
		{
			for(int j=0; j<4; j++)
			{
				ServerStart.player.get(i).setCardInHand(j, deck.getCardFromTop().getId());
			}
			ServerStart.player.get(i).setChips(chips_number);
			sendOne(make_message("Cards", i), i);
		}

		sendAll(make_message_for_all("U_Chips"));
		sendAll(make_message_for_all("Nick"));


	}
	public void setPlayerIndex()
	{
		int k=0;
		for (int i=0; i<ServerStart.player.size(); i++)
		{
			if(ServerStart.player.get(i).equals(null))
			{
				System.out.println("null");
				continue;
			}
			else
			{
				ServerStart.player.get(i).player_id=k;
				k++;
			}
		}
	}
	public void sendAll(String komunikat)
	{
		for(int i=0;i<ServerStart.player.size();i++)
		{
			if(!ServerStart.player.get(i).equals(null))
			{
				ServerStart.player.get(i).out.println(komunikat);
			}
		}
	}
	public void sendOne(String komunikat, int player_id)
	{
		ServerStart.player.get(player_id).out.println(komunikat);
	}

	public String make_message(String code, int player_id)
	{
		String message=" ";
		if(code.equals("Cards"))
		{
			message =
					"Cards "+ String.valueOf(ServerStart.player.get(player_id).getCardFromHand(0).getId())+" "
							+String.valueOf(ServerStart.player.get(player_id).getCardFromHand(1).getId())+" "
							+String.valueOf(ServerStart.player.get(player_id).getCardFromHand(2).getId())+" "
							+String.valueOf(ServerStart.player.get(player_id).getCardFromHand(3).getId())+" ";

		}
		else if(code.equals("Chips"))
		{
			message =
					"Chips " + String.valueOf(ServerStart.player.get(player_id).getChips());
		}
		System.out.println(message);
		return message;
	}

	public String make_message_for_all(String code)
	{
		String message=code;
		if(code.equals("U_Chips"))
		{
			for(int i=0; i<ServerStart.player.size();i++)
			{
				message=message+" "+String.valueOf(i)+" "+String.valueOf(ServerStart.player.get(i).getChips());
			}
		}
		else if(code.equals("Nick"))
		{
			for(int i=0;i<ServerStart.player.size();i++)
			{
				message=message+" "+ServerStart.player.get(i).nickname;
			}
		}

		return message;
	}

	public void delete_player_info(int index)
	{
		sendAll("DeletePlayer "+index);
	}
	
}